# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor
from jms.dim.dim_network_whole_massage import jms_dim__dim_network_whole_massage
label = f"dwm_network_whole_massage_{int(time())}"
timeout = timedelta(minutes=30).seconds

doris_jms_dwm__dwm_network_whole_massage = RichSqlSensor(
    task_id='doris_jms_dwm__dwm_network_whole_massage',
    pool='broker_load_pool',
    depends_on_past=True,
    email=['shenjiaming@jtexpress.com', 'yl_bigdata@yl-scm.com'],
    task_concurrency=1,
    conn_id='doris_real_time3',
    pre_sql=f"""use jms_dwm;
                show load order by createtime desc;
                LOAD LABEL jms_dwm.{label} (
                     DATA INFILE(
                     "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dim.db/external/dim_network_whole_massage/*"
                     ) INTO TABLE dwm_network_whole_massage
                     FORMAT AS 'PARQUET'
                 )
                 WITH BROKER '{{{{ var.json.doris_real_time_brokers_new | random_choice }}}}'
                 PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')
                 """,
    poke_sql=f"SHOW LOAD FROM jms_dwm WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dwm WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(minutes=30), )
doris_jms_dwm__dwm_network_whole_massage << jms_dim__dim_network_whole_massage